Skip to main content

Full text of "Programming"

See other formats

Arduino and Python: Learn Serial Programming 


Make Projects 

build, hack, tweak, share, discover, J 

Arduino and Python: Learn 

Serial Programming 

Written By: Chandler 


Arduino microcontroller (1) 


16x2 LCD (1) 

Speaker (1) 

22 awg wire (1) 

Speaker wire (1) 


For this tutorial, you will need (or at least it is helpful to have) a knowledge of the Python 
programming language. You will also need an Arduino and, depending on how many of the 
projects below you want to work on, different electronic parts. Let's dive in! 

© Make Projects 

Page 1 of 9 

Arduino and Python: Learn Serial Programming 

Universal Feed Parser 

p*rw «55 iiiid Aton* f«ds in PvttMK>. 3W» unit tests, Gp*n source. 

Dawrnlofld - &dg urn en to lion ■ Unit EesLs - Report a bug 

*** 4 - '«*+*■«'■ F»*TtJ * M; ty ; /J* »i dp ■'•■**- ^^fly^i/^AH^lfft/t^pm^. x>4 B | 

»* ot^-Wo"]! 'Utll-I * *•** *E* JJ + A chewy 

u'Sfciftl* P**J 

*■** * f*td, tltt* ■ ##E r#4«*M Jlt-f'-lt/t* *^ ftti-tlfit 

li W;li F<*d- 

i-va. J. chanrWl.'.i.t'lw P «i* ftJS £■' JJEt-r Irrwmalaw *"jTr*» r» 

u'£iapla F—n 1 ' 

M+ d I'Hd Hank P maEwi niitiH Zuvfec 

tf'httt //4-WU *rfl/' 

M* 4 f **4 "WW itl* * JWMF tJJfJM* 1 fTT^ 

iTF** 4ML*fertEi £!£-»< ■■■■fipMj-*/***' 

KM * «haW*V s**K *±£1 ±W ■ RS5 e+ffcifltli**? ■■*****«■ At* 

u'l-a-r "fi'iMnhnl - 1 nr ■■■■or'. >'*■.■"■#■-■ " 

P4* larjdl '«TtroMH~]|i P -wErxvi ■ *■ m Eijf 


■ ■.■ dL'anrtrLiii'JInll "fritl*" | m *n* mEf/ eb j di ftiaoMrv 

y-Ft<"wt «n(F^ tirf*' 

iTFirat *n( rj fcitft* 1 

M* d[-xt«d'IE4| litl* ■ BiK EprB-jTot^TF *.?rL , k, ., j >;! 

j'Fint -miry "lil-" 1 

-»- ■ - d -anf ri*n[Q<] 

*» # Hi** * *Wp »OC*J* tfl +(t*/iMt# 3i^» 

M"httfl/rti^"*A+ Bl^DJf■rltFy/J , 

>» #. llrthftlll . Mrt ■ 'uCI *e*-lv** E*+Il Jltaa liA|.| 

*■*"» ■ lirfca|Qp hr«f P nioLvu rpZijin lud .m ba»r Prc 
u hfclp y.'miamp-'i-m vrgj/vrftrj/l' 

SOUrCEfOrgE I Find Oj»n Source Softwar* 

Python Serial Port Extension by cimta 

Sumnury H.M Amirws Support Drvdup 

Mi;' lipl jtform Pan Module (m Pyiiran (Win32. Jyltian. Linux . BSD And mare) 

FrojKt Horrw 
pyseriial xf.ntiU 

Eurcomnwivd#d By 
IS user; 

CwHap Lilt Updjto 

sf.iwt/ptajctJVp^c-nalra'cvc-lDp 19 hours ago 

Support<1idcfccrr?i , ufic = arfcl& 


Pytrnfi Sartwraic 

faurmauwv License 

Other Versions 
6rniii*»jill filss 

More Cm** 

Spomar Link 

AMD Opttren'" 6100 S*ri+i pf»e«f l*rt 

H*a.H*cr Lag In 


changes lives,,, 
-..will yours? 

^ ^ . 


EpiC Brilliant? We're hiring. 

M', by Co«k" 


Vmpt? installation; 1-16 BWTsFrw 
dpivnlaadnblc drivera 

To start, let's grab your (virtual) parts. Go to: 

and . . . 

so you can download the packages for the two extra libraries we will need, feedparser and 

Note: To install these, unpack the tar.gz file and run like the following (on Linux): 

python install 

On other systems, just run the file with the command line argument "install." 

Now for the physical parts. As you 
can see to the left, all you need for 
this first project are an LED and an 
Arduino. The box and switch are 
part of my Arduino housing and 
aren't required. 

© Make Projects 

Page 2 of 9 

Arduino and Python: Learn Serial Programming 

Now just connect the LED between 
pin 13 (+) and GND (-), and plug 
the Arduino into your computer. 

File Edit Sketch Tools Heip 

©® [5IISEIE1 S 

set up I K 
Serial. begin (9600); 

void loop(){ 
if (serial, road (J •■ «H 
digitalwritetis, high); 
if (Serial. availabVed^H 

Serial .print Ln [Serial . readO) ; 

void setup (){ 
Serial. begin (9600); 


void loop(){ 
if (Serial, read () == 49) { 
digitalWrite(13, HIGH); 


if (Serial. available ()>0){ 

Serial , println (Serial * read () ) ; 

Now, just download this program onto the Arduino. Leave it plugged in after downloading. 

© Make Projects 

Page 3 of 9 

Arduino and Python: Learn Serial Programming 

import serial 

import feedparserj 

import time 

myserial = serial. SeriaK ' /dev/ttyUSBO* f 96G6) 

makezine = feedparser.parse("http://blog.inakezin 

while True; 

makezine latest = makezine. entries [Q] 

title = makezinelatest. title 

myse rial. write (title) 

print title 


myserial . f lushOutput ( ) 

• Then, create a file called 
new_rss . py, or whatever you 
want, ending in M .py". However, 
make sure to take note of what you 
call it. Inside the file, write the text 
in the accompanying image. 

• Note: Sorry, but the image got cut 
off after cropping. The cut-off line 

• makezine = 
feedparser.parse("" ) 

Now, make sure that your Arduino 
is still connected and running the 
Arduino sketch you made earlier. 
Now, run the Python program, and 
when a new feed comes in from the 
Make website, the LED should light 

Note: To view other websites, in 
the line that was cut off in the 
earlier image change the URL to 
the one for the desired website. 
The URL must be that of an 
Atom/RSS feed. 

© Make Projects 

Page 4 of 9 

Arduino and Python: Learn Serial Programming 

Great! On to project #2! Our next 
project goal is to print the title of 
the latest RSS feed entry onto an 
LCD screen. First, assemble the 
parts, plus breadboard and wires. 

Note: The LCD screen is from the 
Maker Shed. 

Then, wire up the LCD screen 
according to the instructions on the 
Maker Shed site. 

© Make Projects 

Page 5 of 9 

Arduino and Python: Learn Serial Programming 

^include <LiquidCrystaL.h> 

String message = " "; 

LiquidCrystal mylcd(7, 8, 9, 10, 11, 12) j 

void setupCH 

Serial. beg in (9600) ; 
mylcd. begin (16, 2)j 

void loop(){ 
while (Serial. available ()==0){ 

if (Serial, available ()>0){ 
while [Serial, available ()>0){ 
message += cha r (Serial, read ()); 
Serial .println (message) ; 


mylcd.setCur$Dr(G, 0); 
mylcd . i r int (message . subst ring (0, 16) ) ; 
mylcd.setCursor(0, 1); 
mylcd. print (message , subst ring (16)); 
message = " " ; 

Let's program it! Go ahead and 
type up the code in the box to the 
left and download it to the Arduino. 
Sorry about the size of the text. 

Step 10 

import serial 

import feedparserj 

import time 

myserial = serial. Serial ( Vdev/ttyUSBO' , 9600) 

makezine = feedparser,parse("http://blog.makezine 

while True: 

makezine_latest = makezine. entries [0] 

title = makezine latest. title 

myserial. write (title) 

print title 

time. sleep (10) 

myserial . f lushOutput ( ) 

Now let's write the Python script. 
This one can also be named 
anything, but for a suggestion, 
something along the lines of 

make_rss_read . py should do. 

Note: Once again, the last line 
accidentally got clipped. Oddly 
enough, the same line got clipped. 
The complete line reads: 

makezine = 
feedparser.parse("" ) 

© Make Projects 

Page 6 of 9 

Arduino and Python: Learn Serial Programming 

Run this code, and the latest RSS 
feed from Make should display! 

Step 12 

Last but not least, number 3! Our 
final goal is to create a virtual 
piano. There will be a GUI 
(graphical user interface) where the 
user clicks buttons to play notes. 

To begin this one, we have some 
physical parts to collect: an 
Arduino and a speaker. 

© Make Projects 

Page 7 of 9 

Arduino and Python: Learn Serial Programming 

To assemble this project, simply 
plug the speaker into the Arduino, 
with the positive pin going to pin 8 
and the negative to ground. Also, 
plug in the Arduino to your 

Serial . begin (9600) ; 

void loop(){ 
if (Serial. read () == 
tone (8, 262, 500) 


55) { 


if (Serial 

tone (8, 
if (Serial 

tone (8, 


if (Serial 
tone (8, 


if (Serial 
tone (8, 


if (serial 
tone(8 J 


if (Serial 
tone £B, 


if (Serial 
tone (8, 


. read ( ) == 
247, 500) 

54) { 

,read() = 

220, 500) 

53) { 

, read() == 
196, 500) 

52) { 

, readO == 
175, 500) 


, readO == 
165, 500) 

50) { 

, readO == 
147, 500) 

49) { 

, readO == 
131, 500) 

43) { 

Now for the Arduino code. 
Download this program onto your 
Arduino, and leave it plugged in. 
Once again, sorry for the small 

© Make Projects 

Page 8 of 9 

Arduino and Python: Learn Serial Programming 

lapcrt w-rliL 

Iroa Tklnter import • 

«r ■ j.triaLS+ri*U ' .'ferfltvUSW - 9**ll 

: 1,13-1 "yApp: 

ff*«t - FrwiutHM 

f f*«f.i)f sd | j 

tt-LT.t - Bur canf True, tc-lI'-C-^ 

i*Lf _C . grid! rm*4 , cal^nHI 

w L f .0. gr id< rntnfl . (otwiiLI 

wlM.jad^*"-*. <*i^rt-.'i 

M-lf.f - Su! tarn IViM-, t+iT-"*"^ 
«-L r .f . gridi 1 ruw-fl , <aiv«ri-a| 
«-Lf .S ■ Bur torn; Tfwe . tfri.t--6\, 
j*lf .&. grtdi rw*4 , fuiwiMI 
Hlf.A ■ Bu(t«l£fr«K, trJ.t" - A" P 
T-rL i" .A. gr ld< royfl . ■zn'ii^n'M 
Mtf.t ■ iu(io*(fi.p*, ipn--(r. 
WL f -t, }!■ lH f *"-d . f«l^n-A| 
vLf.<i - BtHTe*tTr*^ r h-ji--£- 
wLf .(«(fnw«*. «Li*y.Ti 
daf tbMCnielfh! 

f*r k in rinq^U, ]'jI ; 

f-ur 1 Ln rfthgtHl. 14); 
iff wrH ml - i-'l 

cuurid • -itir.ienrf, nLdr^-lj 
CMiMnd ■ heU.KhkB, widlMSj 

f«*W - f+^,(W*, *ld?h-JJ 

CuHBTKf ■ SEtf.toncA, WkdIRa}) 

dtf Mn*E[telf"h 

ffti" x ifi fafQ«\l, 16) : 
s*r. writer 2 '}\ 
d*f torwFiselfh 

for * in r«r*gef 1, 16) ; 

def tarieG[£*lSh 

far x in '-arge 1 1, 16] : 

for x in fanned, J*): 
d*f tones [selfh 

for x in range | i, it) i 
$#r r vrlt«|-4'|i 
def tOMCZfcelf}: 

tar x in ranged* 16]: 
ser. writer? "^ 

ttriH - Ti![} 

appi = B^App(fQOt) 


Now, save the text to the left as digitai_piano . py, or whatever you want, ending in 
".py". (Note that this code was adapted from the tutorial: An Introduction to Tkinter. 
Thanks!) Also, the code is shown in two images. The images, however, have some 
overlaps, so watch out for that. Finally, the first letters of the first three lines are i, f, and s : 
in case it was too hard to tell. 

When you're done with that, execute the file. A window should pop up! To use this, click a 
button and the note will play from the Arduino! 

Also, an image of the final product is shown to the left. 

• Additional notes: If you had any trouble with these projects, go into the Python file and 
make sure that the path to the Arduino (currently /dev/ttyUSBO) is the path to yours. The 
path above only works for Ubuntu, and so you may have to change it around. Also, the 
piano is in its beta version, and has a few glitches. 

• Final note: If these projects interested you, look at the pySerial documentation that can 
easily be found via Google. 

• Please go ahead and modify/tinker with these projects, and create your own. I have 
already created an amazing TVout application that prints off RSS feeds, and did it in about 
one day. Use this tutorial, and post your accomplishments here, at Make: Projects. 

This document was last generated on 2012-10-31 10:32:41 AM. 

© Make Projects 

Page 9 of 9